package com.elvt.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.elvt.server.mapper.MenuMapper;
import com.elvt.server.pojo.Joblevel;
import com.elvt.server.pojo.Menu;
import com.elvt.server.pojo.MenuRole;
import com.elvt.server.pojo.Role;
import com.elvt.server.service.IMenuRoleService;
import com.elvt.server.service.IMenuService;
import com.elvt.server.service.IRoleService;
import com.elvt.server.utils.RespSult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 作者： elvt
 * @Data 时间：2022/5/25
 * @packageName 包路径： ybg_back_end
 * @Desciption 说明： 权限控制器
 * 对角色的增删改查
 */
@RestController
@RequestMapping("/system/basic/permission")
public class PermissionController {

    @Autowired
    private IRoleService roleService;
    @Autowired
   private MenuMapper menuMapper;
    @Autowired
    private IMenuRoleService menuRoleService;
    @ApiOperation(value = "获取所有的")
    @GetMapping
    public List<Role> findPosition(){
        return roleService.list();

    }
    @ApiOperation(value = "添加职位昵称信息")
    @PostMapping
    public RespSult addPosition(@RequestBody Role role){
        if (StringUtils.isEmpty(role.getName()) ||role.getName().equals("")){
            return RespSult.error("输入的信息不能空");
        }
        if (!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        if (roleService.save(role)) {
            return RespSult.success("添加成功！");

        }
        return RespSult.error("添加失败");




    }
    @ApiOperation(value = "删除指定单个昵称信息")
    @DeleteMapping("/role/{id}")
    public RespSult deleteById(@PathVariable Integer id){

        if (roleService.removeById(id)) {
            return RespSult.success("删除成功");

        }
        return RespSult.error("删除失败");
    }
//    @ApiOperation(value = "修改职位昵称信息")
//    @PutMapping
//    public RespSult update(@RequestBody Role role){
//        if (!role.getName().startsWith("ROLE_")){
//            role.setName("ROLE_"+role.getName());
//        }
//        if (roleService.updateById(role)) {
//            return RespSult.success("更新成功！");
//
//        }
//        return RespSult.error("更新失败");
//    }
    @ApiOperation(value = "批量删除！")
    @DeleteMapping
    public RespSult deleteByIds(Integer[] ids){
        if (roleService.removeByIds(Arrays.asList(ids))){
            return RespSult.success("批量删除成功！");
        }
        return RespSult.error("批量删除失败");
    }
    @ApiOperation(value = "查询所有菜单！")
    @GetMapping("/menus")
    public List<Menu> getAllNenus(){

        return menuMapper.getAllNenus();
    }
    @ApiOperation(value = "根据角色id查询菜单id")
    @GetMapping("/mid/{rid}")
    public List<Integer> getMidByRid(@PathVariable Integer rid) {
       return menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid",rid)).stream().map(MenuRole::getMid).collect(Collectors.toList());
    }
    @ApiOperation(value = "根据角色id更新菜单")
    @PutMapping
    public RespSult updateMenuRole(Integer rid,Integer ids[]){

       return menuRoleService.updateMenuRole(rid,ids);

    }



}
